package src.cs2340.todoApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * controls what the text fields and buttons on the register page do
 * @author Rochelle
 */
public class RegisterScreen extends Activity {
	
	//creates text fields that will contain the name, email, username, and password that the user will enter
	EditText name;
	EditText email;
	EditText username;
	EditText password;
	EditText confirmPass;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);		//creates the display using the register.xml layout
		
		//Instantiates text fields using respective ids
		name = (EditText) findViewById(R.id.namefield);
		email = (EditText) findViewById(R.id.emailfield);
		username = (EditText) findViewById(R.id.usernamefield);
		password = (EditText) findViewById(R.id.passwordfield);
		confirmPass = (EditText) findViewById(R.id.confirmPassfield);
		
		//Instantiates buttons using respective ids
		Button login = (Button) findViewById(R.id.register_login_button);
		Button cancel = (Button) findViewById(R.id.register_cancel_button);
		 
		/**
		 * creates a Listener for cancel which sends the user back to the the initial Welcome page
		 */
		cancel.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegisterScreen.this);
				alertDialog.setMessage("Are you sure you want to leave the register screen?");
				
				/**
				 * The code called for the "YES" option
				 */
				alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						/**will return user to welcome screen*/
						Intent intent = new Intent(getApplicationContext(), WelcomeScreen.class);
						finish();
						startActivity(intent);
					}
				});
				
				/**
				 * The code called for the "NO" option
				 */
				alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				})
				
				.show();
			}
		});
		
		/**
		 * creates a Listener for login that will check and verify that the password matches with 
		 * confirm password, if not then user will be sent back to WelcomeScreen, else the user 
		 * will be taken to their account
		 */
		login.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(username.getText().toString().length() < 5 || 
						password.getText().toString().length() < 5){
					AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegisterScreen.this)
						.setTitle("Insufficient info")
						.setMessage("Username and Password are both required and have to be at least" +
							" 5 characters long")
						.setCancelable(false)
						.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which){
								dialog.cancel();
						}
					});
					AlertDialog alert = alertDialog.create();
					alert.show();
				}
				else if(!password.getText().toString().equals(confirmPass.getText().toString())){
					AlertDialog.Builder builder = new AlertDialog.Builder(RegisterScreen.this);
					builder.setMessage("Password does not match, try again")
					       .setCancelable(false)
					       .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
					           @Override
							public void onClick(DialogInterface dialog, int id) {
					        	   dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
				}
				else{//my code ends here. Don't forget about the other bracket (keberhardson3)	
					Account acc = new Account(username.getText().toString(), password.getText().toString(),
							name.getText().toString(), email.getText().toString(), Database.getInstance());
					try {
						acc.register();
						Intent intent = new Intent(getApplicationContext(),UserTaskListScreen.class);
						intent.putExtra("username", username.getText().toString());
						intent.putExtra("password", password.getText().toString());
						finish();
						startActivity(intent);
					} catch (AddAccountException e) {
						// TODO(all): Correct Error Handling required
						// Account did not get registered
						// Possible options: 
						// 1. Display on the screen as a popup and ask them to retry later (rlobo3)
						AlertDialog.Builder builder = new AlertDialog.Builder(RegisterScreen.this);
						builder.setMessage("Password does not match, try again")
						       .setCancelable(false)
						       .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
						           @Override
								public void onClick(DialogInterface dialog, int id) {
						        	   dialog.cancel();
						           }
						       });
						AlertDialog alert = builder.create();
						alert.show();
					}
				}//this one right here (keberhardson3)
			}
		});
	}
}
